package br.com.skytef.bean;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import br.com.skytef.entity.DadosEnvioEntity;

public class ClienteDadosConsulta {

	public String getXml(DadosEnvioEntity clienteDados) {
		String retorno = "";

		try {

			/*-----------------------------------------------------------------------------
			 * Raiz do arquivo
			 */

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("cliente");
			doc.appendChild(rootElement);

			/*-----------------------------------------------------------------------------
			 * Node 1
			 */
			Element node1 = doc.createElement("identificacao_origem");
			rootElement.appendChild(node1);
			/*-----------------------------------------------------------------------------
			 * Filhos
			 */
			String cd_grupo = clienteDados.getClienteOrgem().getCd_grupo();
			String de_login_usu = clienteDados.getClienteOrgem().getDe_login_usu();
			String de_senha_usu = clienteDados.getClienteOrgem().getDe_senha_usu();
			String cd_loja = clienteDados.getClienteOrgem().getCd_loja();

			Element elCd_grupo = doc.createElement("cd_grupo");
			elCd_grupo.appendChild(doc.createTextNode(cd_grupo));
			node1.appendChild(elCd_grupo);

			Element elDe_login_usu = doc.createElement("de_login_usu");
			elDe_login_usu.appendChild(doc.createTextNode(de_login_usu));
			node1.appendChild(elDe_login_usu);

			Element elDe_senha_usu = doc.createElement("de_senha_usu");
			elDe_senha_usu.appendChild(doc.createTextNode(de_senha_usu));
			node1.appendChild(elDe_senha_usu);

			Element elCd_loja = doc.createElement("cd_loja");
			elCd_loja.appendChild(doc.createTextNode(cd_loja));
			node1.appendChild(elCd_loja);

			/*-----------------------------------------------------------------------------
			 * Node 2
			 */
			Element node2 = doc.createElement("identificacao_cliente");
			rootElement.appendChild(node2);
			/*-----------------------------------------------------------------------------
			 * Filhos
			 */
			String nu_cpf = clienteDados.getClienteIdentificacao().getNu_cpf();
			String nu_cartao = clienteDados.getClienteIdentificacao().getNu_cartao();

			Element elNu_cpf = doc.createElement("nu_cpf");
			elNu_cpf.appendChild(doc.createTextNode(nu_cpf));
			node2.appendChild(elNu_cpf);

			Element elNu_cartao = doc.createElement("nu_cartao");
			elNu_cartao.appendChild(doc.createTextNode(nu_cartao));
			node2.appendChild(elNu_cartao);

			/*-----------------------------------------------------------------------------
			 * FIM node 2
			 -----------------------------------------------------------------------------*/

			/*
			 * Grava xml
			 */
			// write the content into xml
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StringWriter stringWriter = new StringWriter();
			StreamResult result = new StreamResult(stringWriter);
			transformer.transform(source, result);

			retorno = stringWriter.toString();
			//System.out.println(retorno);

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
		return retorno;
	}

}
